package bjtu.cit.bookshop.service.impl;

import bjtu.cit.bookshop.daos.PublisherMapper;
import bjtu.cit.bookshop.entities.Publisher;
import bjtu.cit.bookshop.service.IPublisherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by jia19 on 2015/9/22.
 */
@Service("publisherService")
public class PublisherService implements IPublisherService {

    @Resource
    private PublisherMapper publisherMapper = null;

    @Override
    public int createAPublisher(Publisher publisher) {
        return this.publisherMapper.insertSelective(publisher);
    }

    @Override
    public Publisher getPublisherById(int id) {
        return this.publisherMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deletePublisherById(int id) {
        return this.publisherMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updatePublisherInfo(Publisher publisher) {
        return this.publisherMapper.updateByPrimaryKeySelective(publisher);
    }

    @Override
    public List<Publisher> getPublisherList(int num, int offset) {
        List<Publisher> publishers = this.publisherMapper.selectPublisherList();
        int endIndex = offset + num;
        if (offset >= publishers.size()) {
            return null;
        }
        if (endIndex >= publishers.size()) {
            endIndex = publishers.size() - 1;
        }
        return publishers.subList(offset + 1, endIndex);

    }

    @Override
    public List<Publisher> getPublisherList() {
        return this.publisherMapper.selectPublisherList();
    }
}
